package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Dc3Device;
import com.ruoyi.system.domain.mongodb.PointValue;
import com.ruoyi.system.domain.vo.DevicePoint;
import com.ruoyi.system.mapper.Dc3DeviceMapper;
import com.ruoyi.system.service.IDc3DeviceService;
import com.ruoyi.system.service.PointValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-06
 */
@Service
public class Dc3DeviceServiceImpl implements IDc3DeviceService
{
    @Autowired
    private Dc3DeviceMapper deviceMapper;


    @Resource
    private PointValueService pointValueService;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public Dc3Device selectDc3DeviceById(String id)
    {
        return deviceMapper.selectDc3DeviceById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param dc3Device 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<Dc3Device> selectDc3DeviceList(Dc3Device dc3Device)
    {
        return deviceMapper.selectDc3DeviceList(dc3Device);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param dc3Device 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertDc3Device(Dc3Device dc3Device)
    {
        dc3Device.setCreateTime(DateUtils.getNowDate());
        return deviceMapper.insertDc3Device(dc3Device);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param dc3Device 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateDc3Device(Dc3Device dc3Device)
    {
        dc3Device.setUpdateTime(DateUtils.getNowDate());
        return deviceMapper.updateDc3Device(dc3Device);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteDc3DeviceByIds(String[] ids)
    {
        return deviceMapper.deleteDc3DeviceByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteDc3DeviceById(String id)
    {
        return deviceMapper.deleteDc3DeviceById(id);
    }


    /**
     * 根据设备名称获取设备最新的状态数据
     * @param dc3Device
     * @return
     */
    public List<PointValue> getDc3DevicePointValue( Dc3Device dc3Device) {

        List<PointValue> result = new ArrayList<PointValue>();

        List<DevicePoint> devicePointList = deviceMapper.selectDc3DeviceByDeviceName(dc3Device.getDeviceName());

        if (StringUtils.isNotNull(devicePointList)) {

            for (DevicePoint item : devicePointList) {
                PointValue capPointValue = pointValueService.lambdaQuery().eq(PointValue::getDeviceId, item.getDeviceId())
                        .eq(PointValue::getPointId, item.getPointId()).orderByDesc(PointValue::getCreateTime).one();
                if(StringUtils.isNotNull(capPointValue)) {

                    capPointValue.setPointName(item.getPointName());
                    capPointValue.setDeviceName(item.getDeviceName());
                    result.add(capPointValue);
                }
            }
        }
        return result;
    }


}